﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Threading;
using ReflectionStudio.Core;
using ReflectionStudio.Core.Events;
using System.Windows;
using System.Windows.Controls;
namespace WPFExcelReport
{
    public partial class BaseFunction : DispatcherObject
    {
        #region ================Property=====================
        public ExcelX.ExcelLib.ExcelAgent exAgent;
        public OracleCommand orlReadCommand;
        public string UserID = "        ";
        public string WorkingDate;
        public string UserGroup;
        public string MANDANT = "00";
        public string date = "";
        public string year = "";
        public string month = "";
        public string day = "";
        public string SPRACHKZ;
        ArrayList QDecimal = new ArrayList();
        public OracleConnection OracleDB;
        bool blBuildCellMastrix = true;
        public String ReportLayoutType = "ARTICLE";
        public DataTable DTNameMatrix;
        public string Name = "";
        public string ABPOS = "";
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicQueryParamater;
        public DataSet dsResult;
        public Dictionary<string, object> dicParamater;
        public DataSet setResult;
        public DataTable tableResult;
        public DataSet setReturn;   
        public string iDate
        {

            get
            {
                DateTime date = DateTime.Now;
                return date.Year.ToString() + Right("00" + date.Month.ToString(), 2) + Right("00" + date.Day.ToString(), 2);
            }
        }
        public Queue TableQueue = new Queue();
        public delegate void SafeWinFormsThreadDelegate(DataRow dwABPOS);
        public SafeWinFormsThreadDelegate m_UI;
        string ExcelFile = "";
        string Company = "";
        public int ThreadID = 0;
        public DataTable tbPattern;
        public BaseFunction()
        { 
            dicQueryParamater = new Dictionary<string, object>();
            m_UI = new SafeWinFormsThreadDelegate(BuildABPOS);
            SetUPQDecimal();
            NewPatternTable();
        }
        public BaseFunction(OracleConnection _OracleDB, IntexReptServBLClient _proxy, string userID = "        ")
        {
            this.OracleDB = _OracleDB;
            this.proxy = _proxy;
            this.UserID = userID;
            dicQueryParamater = new Dictionary<string, object>();
            m_UI = new SafeWinFormsThreadDelegate(BuildABPOS);
            SetUPQDecimal();
            NewPatternTable();
          
        }
        public BaseFunction(Dictionary<string, object> DicGlobalPara)
        {
            try
            { 
                NewPatternTable();
                dicQueryParamater = new Dictionary<string, object>();
                dicParamater = DicGlobalPara;
                exAgent = new ExcelX.ExcelLib.ExcelAgent(dicParamater);
                exAgent._app.Visible = false;
                // exAgent._app.WindowState =Microsoft.Office.Interop.Excel.XlWindowState.xlMinimized;
                ExcelFile = dicParamater["ExcelFile"] as string;
                ReportLayoutType = dicParamater["DocType"] as string;
                Company = dicParamater["Company"] as string;
                DTNameMatrix = new DataTable(ReportLayoutType);
                DTNameMatrix.Columns.Add("CellName", typeof(System.String));
                DTNameMatrix.Columns.Add("CellRow", typeof(System.Int16));
                DTNameMatrix.Columns.Add("CellCol", typeof(System.Int16));
                blBuildCellMastrix = (bool)dicParamater["blBuildCellMastrix"];
                if (blBuildCellMastrix)
                {
                    exAgent.ExCelNameMatrix = DTNameMatrix;
                    BuildNameMatrix(ReportLayoutType, Company);
                }
                SetUPQDecimal();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void NewPatternTable()
        {
            tbPattern = new DataTable("Pattern");
            tbPattern.Columns.Add("PatternTable", typeof(String));
            tbPattern.Columns.Add("PatternType", typeof(String));
            tbPattern.Columns.Add("PatternKeyName", typeof(String));
            tbPattern.Columns.Add("PatternKey", typeof(String));
        }
        #endregion

        #region ================Tools=====================
        private void SetUPQDecimal()
        {
            QDecimal = new ArrayList();
            #region QDecimal
            QDecimal.Add("VKPNEUDAT");
            QDecimal.Add("VKPAENDDAT");
            QDecimal.Add("VKPLIEFTERM");
            QDecimal.Add("VKPMENGE1");
            QDecimal.Add("VKPWUTERM");
            QDecimal.Add("ABARESQTY");
            QDecimal.Add("FPLANLENG");
            QDecimal.Add("FACTLENG");
            QDecimal.Add("ABPERLDAT");
            QDecimal.Add("VORDERDATE");
            QDecimal.Add("ZUNEUDAT");
            QDecimal.Add("FNPENDDATE");
            QDecimal.Add("FNAENDDATE");
            QDecimal.Add("ABPLIEFDAT");
            QDecimal.Add("ABPSTARTDAT");
            QDecimal.Add("ABPSOLLMENGE1");
            QDecimal.Add("ABPSOLLMENGE5");
            QDecimal.Add("ABPSOLLMENGE8");
            QDecimal.Add("ABPRESMGE");
            QDecimal.Add("ABPRESSTK");
            QDecimal.Add("ABPZUGET");
            QDecimal.Add("ABPZUGSTK");
            QDecimal.Add("ZUNEUDAT");
            QDecimal.Add("SALESREPLAY");
            QDecimal.Add("SBEAMDATE");
            #endregion
        }
        public DateTime GetDate(string sender)
        {
            DateTime date = DateTime.Now;
            try
            {
                int day = System.Convert.ToInt16(Right(sender, 2));
                int year = System.Convert.ToInt16(Left(sender, 4));
                int mon = System.Convert.ToInt16(sender.Substring(4, 2));
                date = new DateTime(year, mon, day);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return date;
        }
        public string GetDateAddDays(string eDate, int LeadTime)
        {
            DateTime date = DateTime.Now;
            string sDate = "";
            try
            {
                int day = System.Convert.ToInt16(Right(eDate, 2));
                int year = System.Convert.ToInt16(Left(eDate, 4));
                int mon = System.Convert.ToInt16(eDate.Substring(4, 2));
                date = new DateTime(year, mon, day);
                date = date.AddDays(LeadTime);
                sDate = date.Year.ToString() + Right("00" + date.Month.ToString(), 2) + Right("00" + date.Day.ToString(), 2);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return sDate;
        }
        public string GetDate(object sender)
        {

            DatePicker snder = (DatePicker)sender;
            DateTime date = (DateTime)snder.SelectedDate;
            string outdate = "";
            try
            {

                if (date != null)
                {
                    string year = date.Year.ToString();
                    string month = Right("00" + date.Month.ToString(), 2);
                    string day = Right("00" + date.Day.ToString(), 2);
                    outdate = year + month + day;
                }
            }

            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return outdate;
        }
        public DataRow GetCellbyQueue(ExcelX.ExcelLib.ExcelAgent exAgent, Queue CellQueue, DataRow dwTarget)
        {
            while (CellQueue.Count > 0)
            {
                string CellName = CellQueue.Dequeue() as string;
                string ColumnName = CellQueue.Dequeue() as string;
                dwTarget[ColumnName] = exAgent.GetStringValue(CellName);
            }
            return dwTarget;
        }
        public string GetGB2312Value(DataRow dwTemp, string nmField)
        {
            byte[] b;
            string filevalue = "";
            b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[nmField].ToString());
            filevalue = Encoding.GetEncoding("gb2312").GetString(b);
            return filevalue;
        }
        public string GetGB2312Value(string strSource, string nmField)
        {
            byte[] b;
            string filevalue = "";
            b = Encoding.GetEncoding("ISO-8859-1").GetBytes(strSource);
            filevalue = Encoding.GetEncoding("gb2312").GetString(b);
            return filevalue;
        }
        public string Right(string str, int num)
        {
            try
            {
                int leng = str.Length;
                str = str.Substring(leng - num, num);
            }
            catch (Exception ex)
            {
            }
            return str;
        }
        public string Left(string str, int num)
        {
            try
            {
                int leng = str.Length;
                str = str + "                                                                                ";
                str = str.Substring(0, num);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return str;
        }
        public DataTable FilterByColor(DataTable dtSource, string colorField, string Color)
        {
            DataTable dtTarget = dtSource.Clone();
            try
            {
                string filevalue = "";
                foreach (DataRow dwTemp in dtSource.Rows)
                {

                    filevalue = GetGB2312Value(dwTemp, colorField);
                    if (filevalue.Trim() == Color.Trim())
                    {
                        dtTarget.Rows.Add().ItemArray = dwTemp.ItemArray;
                    }
                }
            }
            catch (Exception ex)
            {

                Tracer.Error(ex.StackTrace, ex);
            }
            return dtTarget;
        }
        public DataTable ImportDataRow(DataRow DataRowSrc)
        {
            DataTable objectTable = new DataTable();
            objectTable = DataRowSrc.Table.Clone();//必须先复制表的架构，使具有相同的的列或关系！ 
            objectTable.ImportRow(DataRowSrc);//在objectTable中添加一个新行，并将sourceRow的值复制进去,要求表的结构一样！

            //objectTable=DataRowSrc.Table.Copy();
            return objectTable;
        }
        public DataSet ImportDataTable(DataSet dtObjectDataSet, DataTable dtSourceTable)
        {
            try
            {
                dtObjectDataSet.Merge(dtSourceTable, true, MissingSchemaAction.Add);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return dtObjectDataSet;
        }
        public object ImportDataTable(DataSet dtObjectDataSet, DataTable dtSourceTable, bool flag = true)
        {
            try
            {
                dtObjectDataSet.Merge(dtSourceTable, true, MissingSchemaAction.Add);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return dtObjectDataSet;
        }
        public DataSet ImportDataTable(DataSet dtObjectDataSet, DataRow dwSourceRecord)
        {
            DataTable dtObject;
            dtObject = this.ImportDataRow(dwSourceRecord);
            dtObjectDataSet.Merge(dtObject, true, MissingSchemaAction.Add);
            return dtObjectDataSet;
        }
        public void DivideBy10(DataTable dtSource, Queue Field)
        {
            string Field2H = "";
            while (Field.Count > 0)
            {
                Field2H = Field.Dequeue() as string;

                if (dtSource.Columns.Contains(Field2H))
                {
                    foreach (DataRow dwSource in dtSource.Rows)
                    {
                        string value = dwSource[Field2H].ToString().Trim();
                        if (value == "")
                            value = "0";
                        dwSource[Field2H] = System.Convert.ToDouble(value) * 0.1;
                    }
                }

            }
            dtSource.AcceptChanges();
            return;
        }
        public void DivideBy100(DataTable dtSource, Queue Field)
        {
            string Field2H = "";
            string value = "0";
            while (Field.Count > 0)
            {
                Field2H = Field.Dequeue() as string;

                if (dtSource.Columns.Contains(Field2H))
                {
                    foreach (DataRow dwSource in dtSource.Rows)
                    {
                        value = dwSource[Field2H].ToString();
                        if (value.Trim() == "")
                            value = "0";
                        dwSource[Field2H] = System.Convert.ToDouble(value) * 0.01;
                    }
                }

            }
            dtSource.AcceptChanges();
            return;
        }
        public void DivideBy1000(DataTable dtSource, Queue Field)
        {
            string Field2H = "";
            while (Field.Count > 0)
            {
                Field2H = Field.Dequeue() as string;

                if (dtSource.Columns.Contains(Field2H))
                {
                    foreach (DataRow dwSource in dtSource.Rows)
                    {
                        string value = dwSource[Field2H].ToString().Trim();
                        if (value == "")
                            value = "0";
                        dwSource[Field2H] = System.Convert.ToDouble(value) * 0.001;
                    }
                }

            }
            dtSource.AcceptChanges();
            return;
        }

        public void SetDefault(DataTable dtSource)
        {
            foreach (string column in QDecimal)
            {
                if (!dtSource.Columns.Contains(column))
                    continue;
                var query = from DataRow dwSource in dtSource.AsEnumerable()
                            where dwSource[column].ToString().Trim() == ""
                            select dwSource;
                foreach (DataRow dwTemp in query)
                {
                    dwTemp[column] = "0";
                }
            }
        }
        public string GetDataFromSDDAT(string SDSA, string SDKEY)
        {
            string KeyValue = "";
            try
            {
                OracleCommand ora = new OracleCommand();
                ora = OracleDB.CreateCommand();
                ora.CommandText = "SELECT * FROM INTEX.SDDAT WHERE SDSA=:SDSA and SDKEY like '" + SDKEY + "%'";
                ora.Parameters.Add("SDSA", OracleType.NVarChar).Value = SDSA;
                dsResult = Connect2Proxy(ora);

                if (dsResult.Tables.Count > 0)
                {
                    dsResult.Tables[0].TableName = "SDDAT";
                }
                foreach (DataRow dwTemp in dsResult.Tables["SDDAT"].Rows)
                {

                    KeyValue = dwTemp["SDDATEN"].ToString();// GetGB2312Value(dwTemp, "SDDATEN");
                    KeyValue = KeyValue.Substring(1, 30).Trim();
                }

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return KeyValue;
        }
        public string GetSDDAT(string SDSA, string SDKEY)
        {
            string KeyValue = "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";
            try
            {
                OracleCommand ora = new OracleCommand();
                ora = OracleDB.CreateCommand();
                ora.CommandText = "SELECT * FROM INTEX.SDDAT WHERE SDSA=:SDSA and SDKEY like '" + SDKEY + "%'";
                ora.Parameters.Add("SDSA", OracleType.NVarChar).Value = SDSA;
                dsResult = Connect2Proxy(ora);

                if (dsResult.Tables.Count > 0)
                {
                    dsResult.Tables[0].TableName = "SDDAT";
                }
                foreach (DataRow dwTemp in dsResult.Tables["SDDAT"].Rows)
                {
                    KeyValue = dwTemp["SDDATEN"].ToString();// GetGB2312Value(dwTemp, "SDDATEN");
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return KeyValue;
        }
        public string GetCommentsFromTXTIND(string txtitxtart, string txtisind)
        {
            string KeyValue = "";
            string Comments = "";
            txtisind = txtisind + "                                                                                                                   ";
            txtisind = txtisind.Substring(0, 60);
            string str = "";
            try
            {
                OracleCommand or1 = OracleDB.CreateCommand();
                or1.CommandText =
                     "SELECT TXTKPOS,TXTKTEXT FROM INTEX.TXTIND,INTEX.TXTKON"
                     + "  WHERE  TXTIMANDANT=TXTKMANDANT AND TXTKPIND=TXTIPIND "
                 + "AND TXTITXTART=:txtitxtart AND TXTISIND =:txtisind "
                 + " ORDER BY TXTKPOS";
                or1.Parameters.Add("txtitxtart", OracleType.NVarChar).Value = txtitxtart;
                or1.Parameters.Add("txtisind", OracleType.NVarChar).Value = txtisind;
                //'PV912102            0 RD01                                  '
                dsResult = Connect2Proxy(or1);

                if (dsResult.Tables.Count > 0)
                {
                    dsResult.Tables[0].TableName = "TXTKON";

                    foreach (DataRow dwTemp in dsResult.Tables["TXTKON"].Rows)
                    {
                        str = GetGB2312Value(dwTemp, "TXTKTEXT");
                        if (str.Trim() != "")
                        {
                            Comments += str + Environment.NewLine;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

            return Comments;
        }


        public virtual DataSet GetVKPOS(DataRow dwABPOS)
        {
            DataSet set4Return = new DataSet();
            DataSet setLocal = new DataSet();
            DataSet setTemp = new DataSet();
            DataTable tableLocal = null;
            string ABPAUFKRS = dwABPOS["ABPAUFKRS"].ToString();
            string ABPAUFLFD = dwABPOS["ABPAUFLFD"].ToString();
            string ABPAUFPOS = dwABPOS["ABPAUFPOS"].ToString();
            try
            {
                OracleCommand or1 = OracleDB.CreateCommand();
                or1.CommandText = "SELECT ABAVKAUFTYP,ABAVKAUFKRS,ABAVKAUFLFD,ABAVKAUFPOS,VKPNEUDAT,VKPAENDDAT,KDKUNDNA,VKPWUTERM, "
                + "VKPMEEH,nvl(trim(VKPMENGE1),0)/100 as VKPMENGE1, VKPLIEFTERM,Round(ABASANZSTK*ABASSTKLG*0.001,1) AS ABARESQTY,VKPKDARTNR||VKPKDBEZ||VKPKDFARBNR AS STDBATCHNO,'NO' as Encoded"
                    + ", SUBSTR(SDDATEN,2,10) AS VKPPOSTYP"
                    + " FROM ABAUFM M LEFT JOIN VKPOS VKP ON M.ABAVKAUFKRS=VKPAUFKRS AND ABAVKAUFLFD=VKPAUFLFD AND ABAVKAUFPOS=VKPAUFPOS   AND VKPSA<>'AJ'  "
                    + " LEFT JOIN KDSTAMM ON VKPKUNDNR=KDKUNDNR"
                    + " LEFT JOIN SDDAT ON ABAMANDANT = SUBSTR(SDKEY,1,2) "
                     + " AND VKPPOSTYP = SUBSTR(SDKEY,3,3) AND SDSA = '0253' AND SUBSTR(SDKEY,24,1) = 'V' "
                + " WHERE   ABAMANDANT='00' AND ABAAPOS>'0001' "
                + " AND ABAAUFKRS=:ABAAUFKRS "
                + " AND ABAAUFLFD=:ABAAUFLFD"
                + " AND ABAAUFPOS=:ABAAUFPOS";

                or1.Parameters.Add("ABAAUFKRS", OracleType.NVarChar).Value = ABPAUFKRS;
                or1.Parameters.Add("ABAAUFLFD", OracleType.NVarChar).Value = ABPAUFLFD;
                or1.Parameters.Add("ABAAUFPOS", OracleType.NVarChar).Value = ABPAUFPOS;
                setLocal = Connect2Proxy(or1);
                bool flagfound = false;
                while (!flagfound)
                {
                    if (setLocal.Tables.Count > 0)
                    {
                        tableLocal = setLocal.Tables[0];
                        tableLocal.TableName = "ABAUFM";
                    }
                    if (tableLocal == null)
                    {
                        flagfound = true;
                        break;
                    }
                    if (tableLocal.Rows.Count == 0)
                    {
                        flagfound = true;
                        break;
                    }
                    var query = from rowVKPOS in tableLocal.AsEnumerable()
                                where rowVKPOS.Field<string>("ABAVKAUFTYP") == "K"
                                select rowVKPOS;
                    if (query.Count() > 0)
                    {
                        flagfound = true;
                        set4Return.Merge(setLocal);
                    }
                    else
                    {
                        /*
                        if (dwABPOS.RowState == DataRowState.Detached)
                        {
                            flagfound = true;
                            break;
                        }
                        */
                        DataRow rowABPOS = dwABPOS.Table.NewRow();
                        setLocal = new DataSet();
                        foreach (DataRow rowTemp in tableLocal.Copy().Rows)
                        {
                            rowABPOS["ABPAUFKRS"] = rowTemp["ABAVKAUFKRS"];
                            rowABPOS["ABPAUFLFD"] = rowTemp["ABAVKAUFLFD"];
                            rowABPOS["ABPAUFPOS"] = rowTemp["ABAVKAUFPOS"];
                            setTemp = GetVKPOS(rowABPOS);
                            if (setTemp.Tables.Count == 0)
                                flagfound = true;
                            else
                            {
                                setLocal.Merge(setTemp);
                            }
                        }
                    }
                }
                if (set4Return.Tables.Contains("ABAUFM")
                    && flagfound)
                {
                    //Queue Q100 = new Queue();
                    //Q100.Enqueue("VKPMENGE1");
                    //Q100.Enqueue("ABARESQTY");
                    //DivideBy100(set4Return.Tables["ABAUFM"], Q100);
                    byte[] b;
                    string value;
                    string column = "";
                    foreach (DataRow dwTemp in set4Return.Tables["ABAUFM"].Rows)
                    {
                        string Encoded = dwTemp["Encoded"].ToString();
                        if (Encoded == "YES")
                            continue;
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp["KDKUNDNA"].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp["KDKUNDNA"] = value;
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp["STDBATCHNO"].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp["STDBATCHNO"] = value.Replace(" ", "");
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp["VKPPOSTYP"].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp["VKPPOSTYP"] = value.Trim();
                        dwTemp["Encoded"] = "YES";
                    }
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); ;
            }
            return set4Return;
        }
        public string Convert8859P1ToGB2312(string s)
        {
            return System.Text.Encoding.Default.GetString(System.Text.Encoding.GetEncoding("iso-8859-1").GetBytes(s));
        }
        public string ConvertGB2312To8859P1(string s)
        {
            byte[] b;
            string filevalue = "";
            b = Encoding.GetEncoding("gb2312").GetBytes(s);
            filevalue = Encoding.GetEncoding("ISO-8859-1").GetString(b);
            return filevalue;
        }
        public string GetAUFLFD(string ordType = "T6", string KEY = "ABKOPF")
        {
            string OrdNum = "000000";
            OracleCommand or1;
            DataTable tbLocal;
            DataRow dwLocal = null;
            try
            {
                KEY = Left(KEY + "    ", 8);
                string SDKEY = "00" + KEY + ordType + "          00";

                or1 = OracleDB.CreateCommand();
                or1.CommandText = "SELECT * FROM SDDAT  WHERE SDSA='0022' AND SDKEY=:SDKEY";
                or1.Parameters.Add("SDKEY", OracleType.NVarChar).Value = SDKEY;
                setResult = Connect2Proxy(or1);
                if (setResult.Tables.Count > 0)
                {
                    tbLocal = setResult.Tables[0];
                    tbLocal.TableName = "SDDAT";
                    if (tbLocal.Rows.Count > 0)
                    {
                        dwLocal = tbLocal.Rows[0];
                    }
                }
                if (dwLocal == null)
                    return OrdNum;
                string Current = dwLocal["SDDATEN"].ToString().Substring(47, 6);
                long currentnum = System.Convert.ToInt64(Current);
                OrdNum = Right("000000" + (currentnum + 1).ToString(), 6);
                dwLocal["SDDATEN"] = dwLocal["SDDATEN"].ToString().Substring(0, 47) + OrdNum + dwLocal["SDDATEN"].ToString().Substring(53, 172 - 53);
                or1 = OracleDB.CreateCommand();
                or1.CommandText = "UPDATE SDDAT SET SDDATEN=:SDDATEN"
             + " WHERE SDSA='0022' AND SDKEY=:SDKEY";

                or1.Parameters.Add("SDDATEN", OracleType.NVarChar).Value = dwLocal["SDDATEN"].ToString();
                or1.Parameters.Add("SDKEY", OracleType.NVarChar).Value = SDKEY;
                setResult = Connect2Proxy(or1);
               
                or1 = OracleDB.CreateCommand();
                or1.CommandText = "SELECT ABPAUFKRS,ABPAUFLFD,ABPAUFPOS FROM ABPOS WHERE ABPMANDANT=:ABPMANDANT AND ABPAUFKRS=:ABPAUFKRS AND ABPAUFLFD=:ABPAUFLFD";
                or1.Parameters.Add("ABPMANDANT", OracleType.NVarChar).Value = "00";
                or1.Parameters.Add("ABPAUFKRS", OracleType.NVarChar).Value = ordType;
                or1.Parameters.Add("ABPAUFLFD", OracleType.NVarChar).Value = OrdNum;
                setResult = Connect2Proxy(or1);
                if (setResult.Tables[0].Rows.Count > 0)
                {
                    throw new Exception("生产单号码已经存在，若有异常，立即通知管理员");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                Tracer.Error(ex.StackTrace, ex); ;
            }
            return OrdNum;
        }
        public double TONE(double CIN, string CTYPE = "NM")
        {
            double NE = 0.0;
            try
            {
                switch (CTYPE)
                {
                    case "NM":
                        NE = 0.5905 * CIN;
                        break;
                    case "TEX":
                        NE = 590.54 / CIN;
                        break;
                    case "DEN":
                        NE = 5315 / CIN;
                        break;
                    default:
                        NE = CIN;
                        break;
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); ;
            }
            return NE;
        }
        public double MTR1KG(double CIN, string CTYPE = "NM")
        {
            double Meter = 0.0;
            double NE = 0.0;
            try
            {
                NE = TONE(CIN, CTYPE);

                Meter = NE * 840 * 0.9144 / 0.4536;

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); ;
            }
            return Meter;
        }
        #endregion

        public virtual void BuildABPOS(DataRow dwABPOS)
        {

        }
        public DataSet GetWASTAMM(DataRow dwABPOS = null)
        {
            DataSet set4Return = new DataSet();
            DataSet setLocal = new DataSet();
            DataTable tableLocal = null;
            string ABPAUFKRS = "", ABPAUFLFD = "", ABPAUFPOS = "";
            if (dwABPOS != null)
            {
                ABPAUFKRS = dwABPOS["ABPAUFKRS"].ToString();
                ABPAUFLFD = dwABPOS["ABPAUFLFD"].ToString();
                ABPAUFPOS = dwABPOS["ABPAUFPOS"].ToString();
            }
            try
            {
                OracleCommand or1 = OracleDB.CreateCommand();

                or1.CommandText = "SELECT  DISTINCT WAART,WAVAR FROM INTEX.WASTAMM WHERE ROWNUM<10";
                setLocal = Connect2Proxy(or1);

                if (setLocal.Tables.Count > 0)
                {
                    tableLocal = setLocal.Tables[0];
                    tableLocal.TableName = "WASTAMM";
                    Queue Q100 = new Queue();
                    set4Return.Merge(setLocal);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); ;
            }
            return set4Return;
        }
        public DataSet GetTXT(string VKPAUF, string TXTITXTART = "")
        {
            DataSet set4Return = new DataSet();
            DataSet setLocal = new DataSet();
            DataSet setResult = new DataSet();
            DataSet setTemp = new DataSet();
            DataTable tableLocal = null;
            OracleCommand or1;
            try
            {
                or1 = OracleDB.CreateCommand();
                or1.CommandText = "SELECT TRIM(TXTISIND) AS TXTISIND,TXTITXTART,TXTIPIND,TXTKPOS,TXTKTEXT"
                    + " FROM TXTIND T JOIN TXTKON ON TXTIMANDANT=TXTKMANDANT AND  TXTIPIND=TXTKPIND"
                    + " WHERE TXTIMANDANT=:TXTIMANDANT"
                   + " AND TXTISIND LIKE  '%" + VKPAUF + "%' AND TXTITXTART LIKE '%" + "%'"
                    + " ORDER BY 1,2,3,4";
                or1.Parameters.Add("TXTIMANDANT", OracleType.NVarChar).Value = "00";
                setResult = Connect2Proxy(or1);
                tableLocal = setResult.Tables[0];
                tableLocal.TableName = "TXTKON";
                set4Return.Merge(setResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return set4Return;
        }
        /*
        public DataSet InsertAPZUORD(string Mandant, string ART, string VAR, string ARTTYPE, string APKRS, string APNR)
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            OracleCommand or1 = OracleDB.CreateCommand();
            bool flgExist = false;
            try
            {
                #region ===========SELECT=========================
                or1 = OracleDB.CreateCommand();
                VAR = Left(VAR + "   ", 3);
                or1.CommandText = "SELECT * FROM APZUORD WHERE"
                + " APZMANDANT=:APZMANDANT AND  APZART=:APZART AND APZVAR=:APZVAR AND APZANRKRS=:APZANRKRS AND APZALFDNR=:APZALFDNR AND APZSTUFE=:APZSTUFE";
                or1.Parameters.Add("APZMANDANT", OracleType.NVarChar).Value = Mandant;
                or1.Parameters.Add("APZART", OracleType.NVarChar).Value = ART;
                or1.Parameters.Add("APZVAR", OracleType.NVarChar).Value = VAR;
                or1.Parameters.Add("APZSTUFE", OracleType.NVarChar).Value = ARTTYPE;
                or1.Parameters.Add("APZANRKRS", OracleType.NVarChar).Value = APKRS;
                or1.Parameters.Add("APZALFDNR", OracleType.NVarChar).Value = APNR;
                setResult = Connect2Proxy(or1);
                if (setResult.Tables.Count > 0)
                {
                    tbLocal = setResult.Tables[0];
                    tbLocal.TableName = "APZUORD";
                    if (tbLocal.Rows.Count > 0)
                    {
                        setReturn.Merge(setResult);
                        return setReturn;
                    }
                }
                #endregion
                #region ---------------MAP the data----------------------
                dwLocal = setINTEX.APZUORD.NewAPZUORDRow();
                dwLocal["APZMANDANT"] = Mandant;
                dwLocal["APZART"] = ART;
                dwLocal["APZVAR"] = VAR;
                dwLocal["APZSTUFE"] = ARTTYPE;
                dwLocal["APZANRKRS"] = APKRS;
                dwLocal["APZALFDNR"] = APNR;
                dwLocal["APZNEUBEN"] = UserID;
                dwLocal["APZNEUDAT"] = iDate;
                #endregion
                #region-------------------INSERTNEW------------------------------
                if (!flgExist)
                {
                    or1 = OracleDB.CreateCommand();
                    or1.CommandText = "INSERT INTO APZUORD(APZMANDANT, APZKUNDNR, APZWERK, APZABT, APZART, APZVAR, APZANRKRS, APZALFDNR, APZSTUFE, APZSPEZIF, APZSPSTA, APZSPGRD, APZSTDKALK, APZZUSTAND, APZZUSATZ1, APZZUSATZKZ1, APZZUSATZ2, APZZUSATZKZ2, APZZUSATZ3, APZZUSATZKZ3, APZZUSATZ4, APZZUSATZKZ4, APZZUSATZ5, APZZUSATZKZ5, APZZUSATZ6, APZZUSATZKZ6, APZZUSATZ7, APZZUSATZKZ7, APZZUSATZ8, APZZUSATZKZ8, APZZUSATZ9, APZZUSATZKZ9, APZZUSATZ10, APZZUSATZKZ10, APZZUSATZ11, APZZUSATZKZ11, APZZUSATZ12, APZZUSATZKZ12, APZZUSATZ13, APZZUSATZKZ13, APZZUSATZ14, APZZUSATZKZ14, APZZUSATZ15, APZZUSATZKZ15, APZZUSATZ16, APZZUSATZKZ16, APZZUSATZ17, APZZUSATZKZ17, APZZUSATZ18, APZZUSATZKZ18, APZZUSATZ19, APZZUSATZKZ19, APZZUSATZ20, APZZUSATZKZ20, APZZUSATZ21, APZZUSATZKZ21, APZZUSATZ22, APZZUSATZKZ22, APZZUSATZ23, APZZUSATZKZ23, APZZUSATZ24, APZZUSATZKZ24, APZZUSATZ25, APZZUSATZKZ25, APZZUSATZ26, APZZUSATZKZ26, APZZUSATZ27, APZZUSATZKZ27, APZZUSATZ28, APZZUSATZKZ28, APZZUSATZ29, APZZUSATZKZ29, APZZUSATZ30, APZZUSATZKZ30, APZZUSATZ31, APZZUSATZKZ31, APZZUSATZ32, APZZUSATZKZ32, APZZUSATZ33, APZZUSATZKZ33, APZZUSATZ34, APZZUSATZKZ34, APZZUSATZ35, APZZUSATZKZ35, APZZUSATZ36, APZZUSATZKZ36, APZZUSATZ37, APZZUSATZKZ37, APZZUSATZ38, APZZUSATZKZ38, APZZUSATZ39, APZZUSATZKZ39, APZZUSATZ40, APZZUSATZKZ40, APZZUSATZ41, APZZUSATZKZ41, APZZUSATZ42, APZZUSATZKZ42, APZZUSATZ43, APZZUSATZKZ43, APZZUSATZ44, APZZUSATZKZ44, APZZUSATZ45, APZZUSATZKZ45, APZZUSATZ46, APZZUSATZKZ46, APZZUSATZ47, APZZUSATZKZ47, APZZUSATZ48, APZZUSATZKZ48, APZZUSATZ49, APZZUSATZKZ49, APZZUSATZ50, APZZUSATZKZ50, APZZANRKRS1, APZZALFDNR1, APZZAKZ1, APZZANRKRS2, APZZALFDNR2, APZZAKZ2, APZZANRKRS3, APZZALFDNR3, APZZAKZ3, APZZANRKRS4, APZZALFDNR4, APZZAKZ4, APZPRIO, APZAENDZL, APZAENDBEN, APZAENDDAT, APZNEUBEN, APZNEUDAT, APZFILLER)"
                        + "VALUES (:APZMANDANT, '00000000', '   ', '   ', :APZART, :APZVAR,:APZANRKRS, :APZALFDNR, :APZSTUFE, :APZSPEZIF, 'N', '                              ', 'N', '    ', '          ', '  ', '          ', '  ', '          ', '  ', '          ', '  ', '          ', '  ', '          ', '  ', '          ', '  ', '          ', '  ', '          ', '  ', '          ', '  ', '          ', '  ', '          ', '  ', '          ', '  ', '          ', '  ', '          ', '  ', '          ', '  ', '          ', '  ', '          ', '  ', '          ', '  ', '          ', '  ', '          ', '  ', '          ', '  ', '          ', '  ', '          ', '  ', '          ', '  ', '          ', '  ', '          ', '  ', '          ', '  ', '          ', '  ', '          ', '  ', '          ', '  ', '          ', '  ', '          ', '  ', '          ', '  ', '          ', '  ', '          ', '  ', '          ', '  ', '          ', '  ', '          ', '  ', '          ', '  ', '          ', '  ', '          ', '  ', '          ', '  ', '          ', '  ', '          ', '  ', '          ', '  ', '          ', '  ', '          ', '  ', '          ', '  ', '          ', '  ', '  ', '000000', ' ', '  ', '000000', ' ', '  ', '000000', ' ', '  ', '000000', ' ', '0', 0, '        ', 0, :APZNEUBEN, :APZNEUDAT, '                                                 ')";
                    or1.Parameters.Add("APZMANDANT", OracleType.NVarChar).Value = dwLocal["APZMANDANT"];
                    or1.Parameters.Add("APZART", OracleType.NVarChar).Value = dwLocal["APZART"];
                    or1.Parameters.Add("APZVAR", OracleType.NVarChar).Value = dwLocal["APZVAR"];
                    or1.Parameters.Add("APZSTUFE", OracleType.NVarChar).Value = dwLocal["APZSTUFE"];
                    or1.Parameters.Add("APZSPEZIF", OracleType.NVarChar).Value = Left(dwLocal["APZART"].ToString() + "                                                                ", 50);

                    or1.Parameters.Add("APZANRKRS", OracleType.NVarChar).Value = dwLocal["APZANRKRS"];
                    or1.Parameters.Add("APZALFDNR", OracleType.NVarChar).Value = dwLocal["APZALFDNR"];
                    or1.Parameters.Add("APZNEUBEN", OracleType.NVarChar).Value = dwLocal["APZNEUBEN"];
                    or1.Parameters.Add("APZNEUDAT", OracleType.NVarChar).Value = dwLocal["APZNEUDAT"];
                    Connect2Proxy(or1);
                }
                #endregion
                #region ===========SELECT=========================
                or1 = OracleDB.CreateCommand();
                or1.CommandText = "SELECT * FROM APZUORD WHERE"
                + " APZMANDANT=:APZMANDANT AND  APZART=:APZART AND APZVAR=:APZVAR AND APZANRKRS=:APZANRKRS AND APZALFDNR=:APZALFDNR AND APZSTUFE=:APZSTUFE";
                or1.Parameters.Add("APZMANDANT", OracleType.NVarChar).Value = Mandant;
                or1.Parameters.Add("APZART", OracleType.NVarChar).Value = ART;
                or1.Parameters.Add("APZVAR", OracleType.NVarChar).Value = VAR;
                or1.Parameters.Add("APZSTUFE", OracleType.NVarChar).Value = ARTTYPE;
                or1.Parameters.Add("APZANRKRS", OracleType.NVarChar).Value = APKRS;
                or1.Parameters.Add("APZALFDNR", OracleType.NVarChar).Value = APNR;
                setResult = Connect2Proxy(or1);
                if (setResult.Tables.Count > 0)
                {
                    tbLocal = setResult.Tables[0];
                    tbLocal.TableName = "APZUORD";
                }
                #endregion
                setReturn.Merge(setResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        */
        public DataTable BuildNameMatrix(string ReportLayOutType = "ARTICLE", string strCompany = "HLT")
        {
            try
            {
                string filePath = @"C:\intex.w\Report\" + strCompany + "\\" + strCompany + "NameCellMap.xls";
                string xmlfilepath = @"C:\intex.w\Report\" + strCompany + "\\" + strCompany + ReportLayOutType + "ExCelNameMatrix.xml";

                bool still = true;
                int rowi = 1;
                string strName;
                string Rows;
                string Cols;
                if (File.Exists(xmlfilepath))
                {
                    DTNameMatrix.ReadXml(xmlfilepath);
                    DTNameMatrix.AcceptChanges();
                }
                else
                {
                    exAgent.Company = strCompany;
                    exAgent.strExcelFile = ExcelFile;
                    exAgent.Open(filePath);
                    if (ReportLayOutType == "AB45")
                        ReportLayOutType = "PRODWARP";
                    exAgent.SetCurrentSheet(ReportLayOutType + "NameCellMap");
                    while (still)
                    {
                        rowi++;
                        if (exAgent.GetCellValue(rowi, 1) != null)
                        {
                            strName = exAgent.GetCellValue(rowi, 1).ToString().Trim();
                            Rows = exAgent.GetCellValue(rowi, 2).ToString().Trim();
                            Cols = exAgent.GetCellValue(rowi, 3).ToString().Trim();
                            DTNameMatrix.Rows.Add(strName, Rows, Cols);
                        }
                        else
                        {
                            still = false;
                        }
                    }
                    exAgent.SetCurrentSheet(1);
                    DTNameMatrix.AcceptChanges();
                    DTNameMatrix.WriteXml(xmlfilepath);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

            return DTNameMatrix;
        }
        public virtual DataSet GetReservedOrder(DataRow dwABPOS, ref ArrayList listDIOrder, ref ArrayList listPROrder, string OrderNum = "")
        {
            DataSet set4Return = new DataSet();
            DataSet setLocal = new DataSet();
            DataTable tableLocal = null;
            try
            {
                #region source code
                /*
                  SELECT DISTINCT P.ABPAUFKRS as SBAUFKRS,P.ABPAUFLFD AS SBAUFLFD,P.ABPAUFPOS AS SBAUFPOS,LSLIEFNA,LSNAME1 AS SPRODUCER
FROM ABPOS P LEFT JOIN ABAUFM M ON P.ABPAUFART ='VW' AND P.ABPAUFKRS=M.ABAAUFKRS AND P.ABPAUFLFD=M.ABAAUFLFD AND P.ABPAUFPOS=M.ABAAUFPOS
LEFT JOIN ABFOLGE F ON  P.ABPAUFKRS=ABFAUFKRS AND P.ABPAUFLFD= ABFAUFLFD AND P.ABPAUFPOS=F.ABFAUFPOS LEFT JOIN LIEFDAT L ON  F.ABFLIEFNR=L.LSLIEFNR WHERE   M.ABAVKAUFKRS='PR' AND M.ABAVKAUFLFD='100428' AND M.ABAVKAUFPOS='010'
                 */
                #endregion
                string AUFKRS = dwABPOS["ABPAUFKRS"].ToString();
                string AUFLFD = dwABPOS["ABPAUFLFD"].ToString();
                string AUFPOS = dwABPOS["ABPAUFPOS"].ToString();
                if (OrderNum != "")
                {
                    AUFKRS = OrderNum.Substring(0, 2);
                    AUFLFD = OrderNum.Substring(3, 6);
                    AUFPOS = OrderNum.Substring(10, 3);
                }
                if (AUFLFD == "000000")
                {
                    return set4Return;
                }
                OracleCommand or1 = OracleDB.CreateCommand();
                or1.CommandText = "SELECT DISTINCT M.ABAAPOS,P.ABPAUFKRS,P.ABPAUFLFD,P.ABPAUFPOS,P.ABPAUFART"
                + ",ABPART,ABPVAR,ABPFARBE,BSFBEZ"
                    + ",LSLIEFNA,ABPSOLLMENGE1,ABPSOLLMENGE8"
                + ",LSNAME1 AS EXTPRODUCER  ,ABPWVDAT,ABPLIEFDAT,ABPERLDAT,ABPEALFM"
                + ",DI.ABADIAUFKRS,DI.ABADIAUFLFD,DI.ABADIAUFPOS"
                + " FROM ABPOS P "
                + " LEFT JOIN ABAUFM M ON  P.ABPAUFKRS=M.ABAAUFKRS AND P.ABPAUFLFD=M.ABAAUFLFD  AND P.ABPAUFPOS=M.ABAAUFPOS"
                + " LEFT JOIN ABFOLGE F ON  P.ABPAUFKRS=ABFAUFKRS AND P.ABPAUFLFD= ABFAUFLFD  AND P.ABPAUFPOS=F.ABFAUFPOS"
                + " JOIN LIEFDAT L ON  F.ABFLIEFNR=L.LSLIEFNR"
                + " LEFT JOIN ABAUFM DI ON DI.ABAAUFKRS=P.ABPAUFKRS AND DI.ABAAUFLFD=P.ABPAUFLFD AND DI.ABAAUFPOS=P.ABPAUFPOS AND DI.ABAAPOS='0001'"
                + " LEFT JOIN BSFARB F ON ABPART=BSFKART AND  SUBSTR(ABPVAR,0,2)=BSFKVAR AND ABPFARBE=BSFKFARBE"
                + " WHERE M.ABAAPOS>'0001' AND"
                + "  M.ABAVKAUFKRS=:ABPAUFKRS AND M.ABAVKAUFLFD=:ABPAUFLFD AND M.ABAVKAUFPOS=:ABPAUFPOS"
                + " ORDER BY 1,2,3,4";
                or1.Parameters.Add("ABPAUFKRS", OracleType.NVarChar).Value = AUFKRS;
                or1.Parameters.Add("ABPAUFLFD", OracleType.NVarChar).Value = AUFLFD;
                or1.Parameters.Add("ABPAUFPOS", OracleType.NVarChar).Value = AUFPOS;
                setLocal = Connect2Proxy(or1);
                if (setLocal.Tables.Count > 0)
                {
                    tableLocal = setLocal.Tables[0];
                    tableLocal.TableName = "RESERVEDORDER";
                    Queue Q100 = new Queue();
                    Q100.Enqueue("ABPSOLLMENGE1");
                    Q100.Enqueue("ABPSOLLMENGE8");
                    DivideBy100(tableLocal, Q100);
                    byte[] b;
                    string value;
                    string Column = "";
                    foreach (DataRow dwTemp in tableLocal.Rows)
                    {
                        Column = "EXTPRODUCER";
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[Column].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp[Column] = value;
                        Column = "LSLIEFNA";
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[Column].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp[Column] = value;
                        Column = "BSFBEZ";
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[Column].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp[Column] = value;
                    }
                }
                set4Return.Merge(setLocal);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); ;
            }
            return set4Return;
        }
        public virtual DataSet LogIn(string MANDANT, string UsingID, System.Security.SecureString sPassword)
        {
            DataSet setReturn = new DataSet();
            DataSet setResult;
            DataTable tableLocal = null;
            OracleCommand or1;
            try
            {
                IntPtr p = System.Runtime.InteropServices.Marshal.SecureStringToBSTR(sPassword);
                string Password = System.Runtime.InteropServices.Marshal.PtrToStringBSTR(p);
                UsingID = UsingID.Trim().ToLower();
                Password = Password.Trim().ToLower();
                DataSet setHR = GetHRSTAMM();
                var query = from dwHRSTAMM in setHR.Tables["HRSTAMM"].AsEnumerable()
                            where dwHRSTAMM["HRMANDANT"].ToString() == MANDANT
                            && dwHRSTAMM["HRBENID"].ToString().Trim().ToLower() == UsingID
                            && dwHRSTAMM["HRPASSW"].ToString().Trim().ToLower() == Password
                            select dwHRSTAMM;
                foreach (DataRow dwTemp in query)
                {
                    setReturn = ImportDataTable(setReturn, dwTemp);
                }
                return setReturn;
                or1 = OracleDB.CreateCommand();
                UsingID = UsingID.Trim();
                UsingID = Left(UsingID + "        ", 8);
                or1.CommandText = "SELECT * FROM HRSTAMM WHERE HRMANDANT='" + MANDANT + "' AND HRBENID='" + UsingID + "'  ";
                setResult = Connect2Proxy(or1);
                if (setResult.Tables.Count > 0)
                {
                    tableLocal = setResult.Tables[0];
                    tableLocal.TableName = "HRSTAMM";
                    byte[] b;
                    string value;
                    string Column;
                    foreach (DataRow dwTemp in tableLocal.Rows)
                    {
                        Column = "HRVNAME";
                        #region
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[Column].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp[Column] = value;
                        #endregion
                        Column = "HRNNAME";
                        #region
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[Column].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp[Column] = value;
                        #endregion
                    }
                }
                setReturn.Merge(setResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); ;
            }
            return setReturn;
        }
        private DataSet GetHRSTAMM()
        {
            DataSet setReturn = new DataSet();
            OracleCommand or1;
            try
            {
                DataSet setHR = new DataSet();
                string filePath = Path.Combine(PathHelper.ApplicationPath, "HRSTAMM.XML");
                if (System.IO.File.Exists(filePath))
                {
                    setHR.ReadXml(filePath);
                    setHR.Tables[0].TableName = "HRSTAMM";
                    setReturn.Merge(setHR);
                    return setReturn;
                }

                or1 = OracleDB.CreateCommand();
                or1.CommandText = "SELECT HRMANDANT,HRBENID,HRPASSW,HRSPRKZ,HRVNAME,HRNNAME FROM HRSTAMM";
                setResult = Connect2Proxy(or1);
                setResult.Tables[0].TableName = "HRSTAMM";
                setReturn.Merge(setResult);
                setReturn.WriteXml(filePath);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }

        public DataSet LoadSetWEAVE()
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                string filePath = Path.Combine(PathHelper.ApplicationPath, "SETWEAVE.XML");
                if (System.IO.File.Exists(filePath))
                {
                    setResult.ReadXml(filePath);
                    setReturn.Merge(setResult);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        public DataSet QueryWeave(DataSet setWEAVE, string PGAART, string DFAART)
        {
            DataSet setReturn = new DataSet();
            DataRow[] tempPEGPLAN, tempDRAFTING;
            try
            {
                /*
                DataTable tempPEGPLAN = setWEAVE.Tables["PEGPLAN"].Clone(); 
                DataRow dwPEGPLAN = tempPEGPLAN.NewRow();

                var query = from DataRow dwTemp in setWEAVE.Tables["PEGPLAN"].AsEnumerable()
                            where dwTemp["PGAART"].ToString() == PGAART
                            select dwTemp;
                foreach (DataRow dwTemp in query)
                {
                    dwPEGPLAN = tempPEGPLAN.NewRow();
                    dwPEGPLAN.ItemArray = dwTemp.ItemArray;
                    tempPEGPLAN.Rows.Add(dwPEGPLAN);
                }
                */
                tempPEGPLAN = setWEAVE.Tables["PEGPLAN"].Select("PGAART='" + PGAART + "'");
                setReturn.Merge(tempPEGPLAN);


                /*   DataTable tempDRAFTING = setWEAVE.Tables["DRAFTING"].Clone();
                DataRow dwDRAFTING = tempDRAFTING.NewRow();

                var query1 = from DataRow dwTemp in setWEAVE.Tables["DRAFTING"].AsEnumerable()
                             where dwTemp["DFAART"].ToString() == DFAART
                             select dwTemp;
                foreach (DataRow dwTemp in query1)
                {
                    dwDRAFTING = tempDRAFTING.NewRow();
                    dwDRAFTING.ItemArray = dwTemp.ItemArray;
                    tempDRAFTING.Rows.Add(dwDRAFTING);
                }
                */
                tempDRAFTING = setWEAVE.Tables["DRAFTING"].Select("DFAART='" + DFAART + "'");
                setReturn.Merge(tempDRAFTING);

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        public void SaveSetWEAVE(DataSet setWEAVE)
        {

            try
            {
                string filePath = Path.Combine(PathHelper.ApplicationPath, "SETWEAVE.XML");
                if (System.IO.File.Exists(filePath))
                {
                    //   File.Delete(filePath);
                }
                setWEAVE.WriteXml(filePath);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }
    }
}
